//
//  ImageSelectorViewController.swift
//  CMBMobile
//
//  Created by Luo on 5/9/16.
//  Copyright © 2016 Yst－WHB. All rights reserved.
//

import UIKit
import AssetsLibrary
import CoreImage
import Foundation
import Photos


class MTImagePickerAssetsController :UIViewController,UICollectionViewDataSource,UICollectionViewDelegate {
    
    weak var delegate:MTImagePickerDataSourceDelegate!
    var groupModel:MTImagePickerAlbumModel!
    
    @IBOutlet weak var collectionView: MTImagePickerCollectionView!
    @IBOutlet weak var lbSelected: UILabel!
    @IBOutlet weak var btnPreview: UIButton!
    
    private var dataSource = [MTImagePickerModel]()
    private var initialScrollDone:Bool = false
    
    class var instance:MTImagePickerAssetsController {
        get {
            let storyboard = UIStoryboard(name: "MTImagePicker", bundle: Bundle.getResourcesBundle())
            let vc = storyboard.instantiateViewController(withIdentifier: "MTImagePickerController") as! MTImagePickerAssetsController
            return vc
        }
    }
    
    //MARK: Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        if let title = self.groupModel.getAlbumName() {
            self.title = title
        }
        loadPhotos()
    }
    
    func loadPhotos(_ isFirstLoad: Bool = true) {
        var loading: MTLoadingViewController!
        DispatchQueue.main.async {
            if isFirstLoad {
                loading = MTLoadingViewController()
                loading.show(text: "正在加载...")
            }
            DispatchQueue.global().async {
                self.groupModel?.getMTImagePickerModelsListAsync { (models) in
                    DispatchQueue.main.async {
                        loading?.dismiss()
                        if let newImageModel = models.last, self.dataSource.count > 0 {
                            if self.delegate.selectedSource.count < self.delegate.maxCount {
                                self.delegate?.selectedSource.append(newImageModel)
                                self.lbSelected.text = String(self.delegate.selectedSource.count)
                            }
                            else {
                                let alertView = FlashAlertView(message: "已达到最大选择数", delegate: nil)
                                alertView.show()
                            }
                        }
                        if let title = self.groupModel.getAlbumName() {
                            self.title = title
                        }
                        let model = MTImagePickerModel(mediaType: .Photo)
                        model.isAdd = true
                        self.dataSource = models.reversed()
                        self.dataSource.insert(model, at: 0)
                        self.collectionView.reloadData()
                    }
                }
            }
            
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.collectionView.reloadData()
        self.lbSelected.text = String(delegate.selectedSource.count)
        self.btnPreview.isEnabled = !(delegate.selectedSource.count == 0)
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        if !self.initialScrollDone {
            self.initialScrollDone = true
        }
    }
    
    //MARK: UICollectionViewDelegate
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as! MTImagePickerCell
        let model = self.dataSource[indexPath.row]
        if model.mediaType == .Video   {
            cell.videoView.isHidden = false
            model.getVideoDurationAsync(){
                duration in
                DispatchQueue.main.async {
                    cell.videoDuration.text = duration.timeFormat()

                }
            }
        } else {
            cell.videoView.isHidden = true
        }
        
        if model.isAdd {
            cell.imageView.image = nil
            cell.btnCheck.isHidden = true
            cell.addButton.isHidden = false
            cell.addButton.setImage(UIImage(named: "take_photo"), for: .normal)
        }
        else {
            cell.btnCheck.isHidden = false
            cell.addButton.isHidden = true
            cell.imageView.image = model.getThumbImage(size: cell.imageView.frame.size)
            cell.indexPath = indexPath
            cell.btnCheck.isSelected = delegate.selectedSource.contains(model)
            cell.btnCheck.addTarget(self, action: #selector(MTImagePickerAssetsController.btnCheckTouch(_:)), for: .touchUpInside)
        }
        
        cell.leading.constant = self.collectionView.leading.constant
        cell.trailing.constant = self.collectionView.leading.constant
        cell.top.constant = self.collectionView.leading.constant * 2
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let model = self.dataSource[indexPath.row]
        if model.isAdd {
            takePhoto()
        }
        else {
            self.pushToImageSelectorPreviewController(initialIndexPath: indexPath, dataSource: self.dataSource)
        }
    }
    
    @objc func takePhoto() {
        
        let imagePickerVC = UIImagePickerController()
        imagePickerVC.delegate = self
        imagePickerVC.sourceType = .camera
        self.present(imagePickerVC, animated: true, completion: nil)
    }
    
    @objc func btnCheckTouch(_ sender:UIButton) {
        if delegate.selectedSource.count < delegate.maxCount || sender.isSelected == true {
            sender.isSelected = !sender.isSelected
            let index = (sender.superview?.superview as! MTImagePickerCell).indexPath.row
            if sender.isSelected {
               delegate.selectedSource.append(self.dataSource[index])
                sender.heartbeatsAnimation(duration: 0.15)
            }else {
                if let removeIndex = delegate.selectedSource.firstIndex(of: self.dataSource[index]) {
                    delegate.selectedSource.remove(at: removeIndex)
                }
            }
            self.lbSelected.text = String(delegate.selectedSource.count)
            self.lbSelected.heartbeatsAnimation(duration: 0.15)
            self.btnPreview.isEnabled = !(delegate.selectedSource.count == 0)
        } else {
            let alertView = FlashAlertView(message: "已达到最大选择数", delegate: nil)
            alertView.show()
        }
    }
    
    //旋转处理
    override func willRotate(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) {
        if self.interfaceOrientation.isPortrait != toInterfaceOrientation.isPortrait {
            self.collectionView.prevItemSize = (self.collectionView.collectionViewLayout as! MTImagePickerFlowLayout).itemSize
            self.collectionView.prevOffset = self.collectionView.contentOffset.y
            self.collectionView.collectionViewLayout.invalidateLayout()
        }
    }
    
    //MARK: private methods
    
    private func pushToImageSelectorPreviewController(initialIndexPath:IndexPath?,dataSource:[MTImagePickerModel]) {
        guard let initialIndexPath = initialIndexPath  else {
            return
        }
        var indexPath = initialIndexPath
        indexPath.row -= 1
        let vc = MTImagePickerPreviewController.instance
        var dataSource = dataSource
        dataSource.removeFirst()
        vc.dataSource = dataSource
        vc.delegate = self.delegate
        vc.initialIndexPath = indexPath
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    //MARK: IBActions
    @IBAction func btnFinishTouch(_ sender: AnyObject) {
        delegate.didFinishPicking()
    }
  
    @IBAction func btnPreviewTouch(_ sender: AnyObject) {
//        let dataSource = delegate.selectedSource
        //self.pushToImageSelectorPreviewController(initialIndexPath: nil, dataSource: dataSource)
    }
    @IBAction func btnCancelTouch(_ sender: AnyObject) {
        delegate.didCancel()
    }
}

class MTImagePickerCollectionView:UICollectionView {
    @IBOutlet weak var trailing: NSLayoutConstraint!
    @IBOutlet weak var leading: NSLayoutConstraint!
    var prevItemSize:CGSize?
    var prevOffset:CGFloat = 0
}
// MARK: - UIImagePickerControllerDelegate
extension MTImagePickerAssetsController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    internal func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            
            PHPhotoLibrary.shared().performChanges({
                
                PHAssetChangeRequest.creationRequestForAsset(from: image)
                
            }) {  [weak self](success, e) in
                if success {
                    DispatchQueue.main.async {
                        if let nav = self?.navigationController as? MTImagePickerController {
                            MTImagePickerDataSource.fetchDefault(type: nav.source, mediaTypes: nav.mediaTypes) {
                                self?.groupModel = $0
                                self?.loadPhotos(false)
                            }
                        }
                    }
                }
               
            }
        }
        DispatchQueue.main.async {
            picker.dismiss(animated: true, completion: nil)
        }
    }
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }
    
}
